import {LightShadow} from './LightShadow.js'
import {MathUtils} from '../math/MathUtils.js'
import {PerspectiveCamera} from '../cameras/PerspectiveCamera.js'

function SpotLightShadow() {
  LightShadow.call(this, new PerspectiveCamera(50, 1, 0.5, 500))

  this.focus = 1
}

SpotLightShadow.prototype = Object.assign(Object.create(LightShadow.prototype), {
  constructor: SpotLightShadow,

  isSpotLightShadow: true,

  updateMatrices: function (light) {
    const camera = this.camera

    const fov = MathUtils.RAD2DEG * 2 * light.angle * this.focus
    const aspect = this.mapSize.width / this.mapSize.height
    const far = light.distance || camera.far

    if (fov !== camera.fov || aspect !== camera.aspect || far !== camera.far) {
      camera.fov = fov
      camera.aspect = aspect
      camera.far = far
      camera.updateProjectionMatrix()
    }

    LightShadow.prototype.updateMatrices.call(this, light)
  },
})

export {SpotLightShadow}
